home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / editors / mntemacs.zoo / src / keyboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  56.7 KB  |  2,272 lines

  1. /* Keyboard input; editor command loop.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*** For version 19, can simplify this by making interrupt_input 1 on VMS.  */
  21.  
  22. /* Allow config.h to undefine symbols found here.  */
  23. #include <signal.h>
  24.  
  25. #include "config.h"
  26. #include <stdio.h>
  27. #undef NULL
  28. #include "termchar.h"
  29. #include "termopts.h"
  30. #include "termhooks.h"
  31. #include "lisp.h"
  32. #include "macros.h"
  33. #include "window.h"
  34. #include "commands.h"
  35. #include "buffer.h"
  36. #include <setjmp.h>
  37. #include <errno.h>
  38.  
  39. extern int errno;
  40.  
  41. /* Get FIONREAD, if it is available.  */
  42. #ifdef USG
  43. #include <termio.h>
  44. #include <fcntl.h>
  45. #else /* not USG */
  46. #ifndef VMS
  47. #include <sys/ioctl.h>
  48. #endif /* not VMS */
  49. #endif /* not USG */
  50.  
  51. /* Allow m- file to inhibit use of FIONREAD.  */
  52. #ifdef BROKEN_FIONREAD
  53. #undef FIONREAD
  54. #endif
  55.  
  56. /* Make all keyboard buffers much bigger when using X windows.  */
  57. #ifdef HAVE_X_WINDOWS
  58. #define BUFFER_SIZE_FACTOR 16
  59. #else
  60. #define BUFFER_SIZE_FACTOR 1
  61. #endif
  62.  
  63. /* Following definition copied from eval.c */
  64.  
  65. struct backtrace
  66.   {
  67.     struct backtrace *next;
  68.     Lisp_Object *function;
  69.     Lisp_Object *args;    /* Points to vector of args. */
  70.     int nargs;        /* length of vector */
  71.            /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
  72.     char evalargs;
  73.   };
  74.  
  75. /* Non-nil disable property on a command means
  76.  do not execute it; call disabled-command-hook's value instead. */
  77. Lisp_Object Qdisabled, Vdisabled_command_hook;
  78.  
  79. int recent_keys_index;    /* Index for storing next element into recent_keys */
  80. int total_keys;        /* Total number of elements stored into recent_keys */
  81. char recent_keys[100];    /* Holds last 100 keystrokes */
  82.  
  83. /* Buffer holding the key that invoked the current command.  */
  84. char *this_command_keys;
  85. int this_command_key_count;    /* Size in use.  */
  86. int this_command_keys_size;    /* Size allocated.  */
  87.  
  88. extern struct backtrace *backtrace_list;
  89.  
  90. static jmp_buf getcjmp;    /* for longjmp to where kbd input is being done. */
  91.  
  92. int waiting_for_input;    /* True while doing kbd input */
  93.  
  94. /* True while displaying for echoing.   Delays C-g throwing. */
  95. static int echoing;
  96.  
  97. int immediate_quit;    /* Nonzero means C-G should cause immediate error-signal. */
  98.  
  99. int help_char;        /* Character to recognize as the help char.  */
  100.  
  101. Lisp_Object Vhelp_form;    /* Form to execute when help char is typed.  */
  102.  
  103. /* Character that causes a quit.  Normally C-g.  */
  104.  
  105. int quit_char;
  106.  
  107. extern Lisp_Object global_map;
  108.  
  109. /* Current depth in recursive edits.  */
  110.  
  111. int command_loop_level;
  112.  
  113. /* Last input character read as a command.  */
  114.  
  115. int last_command_char;
  116.  
  117. /* Last input character read for any purpose.  */
  118.  
  119. int last_input_char;
  120.  
  121. /* If not -1, a character to be read as the next command input */
  122.  
  123. int unread_command_char;
  124.  
  125. /* Char to use as prefix when a meta character is typed in.
  126.  This is bound on entry to minibuffer in case Esc is changed there.  */
  127.  
  128. int meta_prefix_char;
  129.  
  130. /* Total number of times read_command_char has returned.  */
  131.  
  132. int num_input_chars;
  133.  
  134. /* Auto-save automatically when this many characters have been typed
  135.    since the last time.  */
  136.  
  137. static int auto_save_interval;
  138.  
  139. /* Value of num_input_chars as of last auto save.  */
  140.  
  141. int last_auto_save;
  142.  
  143. /* Last command executed by the editor command loop, not counting
  144.    commands that set the prefix argument.  */
  145.  
  146. Lisp_Object last_command;
  147.  
  148. /* The command being executed by the command loop.
  149.    Commands may set this, and the value set will be copied into last_command
  150.    instead of the actual command.  */
  151. Lisp_Object this_command;
  152.  
  153. Lisp_Object Qself_insert_command;
  154. Lisp_Object Qforward_char;
  155. Lisp_Object Qbackward_char;
  156.  
  157. /* read_key_sequence stores here the command definition of the
  158.    key sequence that it reads.  */
  159. Lisp_Object read_key_sequence_cmd;
  160.  
  161. /* Form to evaluate (if non-nil) when Emacs is started */
  162. Lisp_Object Vtop_level;
  163.  
  164. /* User-supplied string to translate input characters through */
  165. Lisp_Object Vkeyboard_translate_table;
  166.  
  167. FILE *dribble;            /* File in which we write all commands we read */
  168.  
  169. /* Nonzero if input is available */
  170. int input_pending;
  171.  
  172. /* Nonzero if should obey 0200 bit in input chars as "Meta" */
  173. int meta_key;
  174.  
  175. extern char *pending_malloc_warning;
  176.  
  177. /* Buffer for pre-read keyboard input */
  178. unsigned char kbd_buffer [256 * BUFFER_SIZE_FACTOR];
  179.  
  180. /* Number of characters available in kbd_buffer.  */
  181. int kbd_count;
  182.  
  183. /* Pointer to next available character in kbd_buffer.  */
  184. unsigned char *kbd_ptr;
  185.  
  186. /* Address (if not 0) of word to zero out
  187.  if a SIGIO interrupt happens */
  188. long *input_available_clear_word;
  189.  
  190. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  191.    Default is 1 if INTERRUPT_INPUT is defined.  */
  192.  
  193. int interrupt_input;
  194.  
  195. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  196.  
  197. int interrupts_deferred;
  198.  
  199. /* nonzero means use ^S/^Q for flow control.  */
  200.  
  201. int flow_control;
  202.  
  203. #ifndef BSD4_1
  204. #define sigfree() sigsetmask (0)
  205. #define sigholdx(sig) sigsetmask (1 << ((sig) - 1))
  206. #define sigblockx(sig) sigblock (1 << ((sig) - 1))
  207. #define sigunblockx(sig) sigblock (0)
  208. #define sigpausex(sig) sigpause (0)
  209. #endif /* not BSD4_1 */
  210.  
  211. #ifdef BSD4_1
  212. #define SIGIO SIGTINT
  213. /* sigfree and sigholdx are in sysdep.c */
  214. #define sigblockx(sig) sighold (sig)
  215. #define sigunblockx(sig) sigrelse (sig)
  216. #define sigpausex(sig) sigpause (sig)
  217. #endif /* BSD4_1 */
  218.  
  219. #ifndef sigmask
  220. #define sigmask(no) (1L << ((no) - 1))
  221. #endif
  222.  
  223. /* We are unable to use interrupts if FIONREAD is not available,
  224.    so flush SIGIO so we won't try. */
  225. #ifndef FIONREAD
  226. #ifdef SIGIO
  227. #undef SIGIO
  228. #endif
  229. #endif
  230.  
  231. /* If we support X Windows, and won't get an interrupt when input
  232.    arrives from the server, poll periodically so we can detect C-g.  */
  233. #ifdef HAVE_X_WINDOWS
  234. #ifndef SIGIO
  235. #define POLL_FOR_INPUT
  236. #endif
  237. #endif
  238.  
  239. /* Function for init_keyboard to call with no args (if nonzero).  */
  240. void (*keyboard_init_hook) ();
  241.  
  242. static void read_avail_input ();
  243. static void get_input_pending ();
  244.  
  245. /* Non-zero tells input_available_signal to call read_socket_hook
  246.    even if FIONREAD returns zero.  */
  247. static int force_input;
  248.  
  249. static int echo_keystrokes;    /* > 0 if we are to echo keystrokes */
  250.  
  251. /* Nonzero means echo each character as typed.  */
  252. static int immediate_echo;
  253.  
  254. #define    min(a,b)    ((a)<(b)?(a):(b))
  255. #define    max(a,b)    ((a)>(b)?(a):(b))
  256.  
  257. static char echobuf[100];
  258. static char *echoptr;
  259.  
  260. /* Install the string STR as the beginning of the string of echoing,
  261.    so that it serves as a prompt for the next character.
  262.    Also start echoing.  */
  263.  
  264. echo_prompt (str)
  265.      char *str;
  266. {
  267.   int len = strlen (str);
  268.   if (len > sizeof echobuf - 4)
  269.     len = sizeof echobuf - 4;
  270.   bcopy (str, echobuf, len + 1);
  271.   echoptr = echobuf + len;
  272.  
  273.   echo ();
  274. }
  275.  
  276. /* Add the character C to the echo string,
  277.    if echoing is going on.  */
  278.  
  279. echo_char (c)
  280.      int c;
  281. {
  282.   extern char *push_key_description ();
  283.  
  284.   if (immediate_echo)
  285.     {
  286.       char *ptr = echoptr;
  287.  
  288.       if (ptr - echobuf > sizeof echobuf - 6)
  289.     return;
  290.  
  291.       ptr = push_key_description (c, ptr);
  292.       *ptr++ = ' ';
  293.       if (echoptr == echobuf && c == help_char)
  294.     {
  295.       strcpy (ptr, "(Type ? for further options) ");
  296.       ptr += strlen (ptr);
  297.     }
  298.  
  299.       *ptr = 0;
  300.       echoptr = ptr;
  301.  
  302.       echo ();
  303.     }
  304. }
  305.  
  306. /* Temporarily add a dash to the end of the echo string,
  307.    so that it serves as a mini-prompt for the very next character.  */
  308.  
  309. echo_dash ()
  310. {
  311.   if (!immediate_echo && echoptr == echobuf)
  312.     return;
  313.  
  314.   /* Put a dash at the end of the buffer temporarily,
  315.      but make it go away when the next character is added.  */
  316.   echoptr[0] = '-';
  317.   echoptr[1] = 0;
  318.  
  319.   echo ();
  320. }
  321.  
  322. /* Display the current echo string, and begin echoing if not already
  323.    doing so.  */
  324.  
  325. echo ()
  326. {
  327.   if (!immediate_echo)
  328.     {
  329.       int i;
  330.       immediate_echo = 1;
  331.  
  332.       for (i = 0; i < this_command_key_count; i++)
  333.     echo_char (this_command_keys[i]);
  334.       echo_dash ();
  335.     }
  336.  
  337.   echoing = 1;
  338.   message1 (echobuf);
  339.   echoing = 0;
  340.  
  341.   if (waiting_for_input && !NULL (Vquit_flag))
  342.     quit_throw_to_read_command_char ();
  343. }
  344.  
  345. /* Turn off echoing, for the start of a new command.  */
  346.  
  347. cancel_echoing ()
  348. {
  349.   immediate_echo = 0;
  350.   echoptr = echobuf;
  351. }
  352.  
  353. /* When an auto-save happens, record the "time", and don't do again soon.  */
  354. record_auto_save ()
  355. {
  356.   last_auto_save = num_input_chars;
  357. }
  358.  
  359. Lisp_Object recursive_edit_unwind (), command_loop ();
  360.  
  361. DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
  362.   "Invoke the editor command loop recursively.\n\
  363. Do (throw 'exit nil) within the command loop to make this function return,\n\
  364. or (throw 'exit t) to make this function signal an error.\n\
  365. This function is called by the editor initialization\n\
  366. to begin editing.")
  367.   ()
  368. {
  369.   int count = specpdl_ptr - specpdl;
  370.  
  371.   command_loop_level++;
  372.   update_mode_lines = 1;
  373.  
  374.   record_unwind_protect (recursive_edit_unwind,
  375.              (current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)
  376.               ? Fcurrent_buffer ()
  377.               : Qnil));
  378.  
  379.   recursive_edit_1 ();
  380.  
  381.   unbind_to (count);
  382.   return Qnil;
  383. }
  384.  
  385. Lisp_Object
  386. recursive_edit_1 ()
  387. {
  388.   int count = specpdl_ptr - specpdl;
  389.   Lisp_Object val;
  390.  
  391.   if (command_loop_level > 0)
  392.     {
  393.       specbind (Qstandard_output, Qt);
  394.       specbind (Qstandard_input, Qt);
  395.     }
  396.  
  397.   val = command_loop ();
  398.   if (EQ (val, Qt))
  399.     Fsignal (Qquit, Qnil);
  400.  
  401.   unbind_to (count);
  402.   return Qnil;
  403. }
  404.  
  405. Lisp_Object
  406. recursive_edit_unwind (buffer)
  407.      Lisp_Object buffer;
  408. {
  409.   if (!NULL (buffer))
  410.     Fset_buffer (buffer);
  411.   command_loop_level--;
  412.   update_mode_lines = 1;
  413.   return Qnil;
  414. }
  415.  
  416. Lisp_Object
  417. cmd_error (data)
  418.      Lisp_Object data;
  419. {
  420.   Lisp_Object errmsg, tail, errname, file_error;
  421.   struct gcpro gcpro1;
  422.   int i;
  423.  
  424.   Vquit_flag = Qnil;
  425.   Vinhibit_quit = Qt;
  426.   Vstandard_output = Qt;
  427.   Vstandard_input = Qt;
  428.   Vexecuting_macro = Qnil;
  429.   echo_area_contents = 0;
  430.  
  431.   Fdiscard_input ();
  432.   bell ();
  433.  
  434.   errname = Fcar (data);
  435.  
  436.   if (EQ (errname, Qerror))
  437.     {
  438.       data = Fcdr (data);
  439.       if (!CONSP (data)) data = Qnil;
  440.       errmsg = Fcar (data);
  441.       file_error = Qnil;
  442.     }
  443.   else
  444.     {
  445.       errmsg = Fget (errname, Qerror_message);
  446.       file_error = Fmemq (Qfile_error,
  447.               Fget (errname, Qerror_conditions));
  448.     }
  449.  
  450.   /* Print an error message including the data items.
  451.      This is done by printing it into a scratch buffer
  452.      and then making a copy of the text in the buffer. */
  453.   
  454.   if (!CONSP (data)) data = Qnil;
  455.   tail = Fcdr (data);
  456.   GCPRO1 (tail);
  457.  
  458.   /* For file-error, make error message by concatenating
  459.      all the data items.  They are all strings.  */
  460.   if (!NULL (file_error))
  461.     errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
  462.  
  463.   if (XTYPE (errmsg) == Lisp_String)
  464.     Fprinc (errmsg, Qt);
  465.   else
  466.     write_string_1 ("peculiar error", -1, Qt);
  467.  
  468.   for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
  469.     {
  470.       write_string_1 (i ? ", " : ": ", 2, Qt);
  471.       if (!NULL (file_error))
  472.     Fprinc (Fcar (tail), Qt);
  473.       else
  474.     Fprin1 (Fcar (tail), Qt);
  475.     }
  476.   UNGCPRO;
  477.  
  478.   /* In -batch mode, force out the error message and newlines after it
  479.      and then die.  */
  480.   if (noninteractive)
  481.     {
  482.       message ("");
  483.       Fkill_emacs (make_number (-1));
  484.     }
  485.  
  486.   Vquit_flag = Qnil;
  487.  
  488.   Vinhibit_quit = Qnil;
  489.   return make_number (0);
  490. }
  491.  
  492. Lisp_Object command_loop_1 ();
  493. Lisp_Object command_loop_2 ();
  494. Lisp_Object cmd_error ();
  495. Lisp_Object top_level_1 ();
  496.  
  497. /* Entry to editor-command-loop.
  498.    This level has the catches for exiting/returning to editor command loop.
  499.    It returns nil to exit recursive edit, t to abort it.  */
  500.  
  501. Lisp_Object
  502. command_loop ()
  503. {
  504.   if (command_loop_level > 0 || minibuf_level > 0)
  505.     {
  506.       return internal_catch (Qexit, command_loop_2, Qnil);
  507.     }
  508.   else
  509.     while (1)
  510.       {
  511.     internal_catch (Qtop_level, top_level_1, Qnil);
  512.     internal_catch (Qtop_level, command_loop_2, Qnil);
  513.     /* End of file in -batch run causes exit here.  */
  514.     if (noninteractive)
  515.       Fkill_emacs (Qt);
  516.       }
  517. }
  518.  
  519. /* Here we catch errors in execution of commands within the
  520.    editing loop, and reenter the editing loop.
  521.    When there is an error, cmd_error runs and returns a non-nil
  522.    value to us.  A value of nil means that cmd_loop_1 itself
  523.    returned due to end of file (or end of kbd macro).  */
  524.  
  525. Lisp_Object
  526. command_loop_2 ()
  527. {
  528.   register Lisp_Object val;
  529.   do
  530.     val = internal_condition_case (command_loop_1, Qerror, cmd_error);
  531.   while (!NULL (val));
  532.   return Qnil;
  533. }
  534.  
  535. Lisp_Object
  536. top_level_2 ()
  537. {
  538.   return Feval (Vtop_level);
  539. }
  540.  
  541. Lisp_Object
  542. top_level_1 ()
  543. {
  544.   /* On entry to the outer level, run the startup file */
  545.   if (!NULL (Vtop_level))
  546.     internal_condition_case (top_level_2, Qerror, cmd_error);
  547.   else if (!NULL (Vpurify_flag))
  548.     message ("Bare impure Emacs (standard Lisp code not loaded)");
  549.   else
  550.     message ("Bare Emacs (standard Lisp code not loaded)");
  551.   return Qnil;
  552. }
  553.  
  554. DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
  555.   "Exit all recursive editing levels.")
  556.   ()
  557. {
  558.   Fthrow (Qtop_level, Qnil);
  559. }
  560.  
  561. DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
  562.   "Exit from the innermost recursive edit or minibuffer.")
  563.   ()
  564. {
  565.   if (command_loop_level > 0 || minibuf_level > 0)
  566.     Fthrow (Qexit, Qnil);
  567.   error ("No recursive edit is in progress");
  568. }
  569.  
  570. DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
  571.   "Abort the command that requested this recursive edit or minibuffer input.")
  572.   ()
  573. {
  574.   if (command_loop_level > 0 || minibuf_level > 0)
  575.     Fthrow (Qexit, Qt);
  576.   error ("No recursive edit is in progress");
  577. }
  578.  
  579. /* This is the actual command reading loop,
  580.  sans error-handling encapsulation */
  581.  
  582. Lisp_Object Fcommand_execute ();
  583.  
  584. Lisp_Object
  585. command_loop_1 ()
  586. {
  587.   Lisp_Object cmd;
  588.   int lose;
  589.   int nonundocount;
  590.   char keybuf[30];
  591.   int i;
  592.   int no_redisplay;
  593.   int no_direct;
  594.  
  595.   Vprefix_arg = Qnil;
  596.   waiting_for_input = 0;
  597.   cancel_echoing ();
  598.   last_command = Qt;
  599.   nonundocount = 0;
  600.   no_redisplay = 0;
  601.   this_command_key_count = 0;
  602.   
  603.   while (1)
  604.     {
  605.       /* Install chars successfully executed in kbd macro */
  606.       if (defining_kbd_macro && NULL (Vprefix_arg))
  607.     finalize_kbd_macro_chars ();
  608.  
  609.       /* Make sure current window's buffer is selected.  */
  610.  
  611.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  612.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  613.  
  614.       /* Display any malloc warning that just came out.
  615.      Use while because displaying one warning can cause another.  */
  616.       while (pending_malloc_warning)
  617.     display_malloc_warning ();
  618.  
  619.       no_direct = 0;
  620.  
  621.       /* If minibuffer on and echo area in use,
  622.      wait 2 sec and redraw minibufer.  */
  623.  
  624.       if (minibuf_level && echo_area_contents)
  625.     {
  626.       int count = specpdl_ptr - specpdl;
  627.       specbind (Qinhibit_quit, Qt);
  628.       Fsit_for (make_number (2), Qnil);
  629.       unbind_to (count);
  630.  
  631.       echo_area_contents = 0;
  632.       no_direct = 1;
  633.       if (!NULL (Vquit_flag))
  634.         {
  635.           Vquit_flag = Qnil;
  636.           unread_command_char = quit_char;
  637.         }
  638.     }
  639.  
  640.       i = 0;
  641. #if 0
  642.       /* If prev. command was directly displayed, we don't need
  643.      redisplay.  Try shortcut for reading single-char key sequence.  */
  644.       if (no_redisplay)
  645.     i = fast_read_one_key (keybuf);
  646. #endif /* 0 */
  647.       /* Shortcut not applicable or found a prefix key.
  648.      Take full precautions and read key sequence the hard way.  */
  649.       if (i == 0)
  650.     {
  651. #ifdef C_ALLOCA
  652.       alloca (0);        /* Cause a garbage collection now */
  653.                 /* Since we can free the most stuff here.  */
  654. #endif /* C_ALLOCA */
  655.  
  656.       /* Read next key sequence; i gets its length.  */
  657.  
  658.       i = read_key_sequence (keybuf, sizeof keybuf, 0,
  659.                  no_redisplay && buffer_shared <= 1);
  660.     }
  661.  
  662.       /* Now we have read a key sequence of length I,
  663.      or else I is 0 and we found end of file.  */
  664.  
  665.       if (i == 0)        /* End of file -- happens only in */
  666.     return Qnil;        /* a kbd macro, at the end */
  667.  
  668.       last_command_char = keybuf[i - 1];
  669.       
  670.       cmd = read_key_sequence_cmd;
  671.       if (!NULL (Vexecuting_macro))
  672.     {
  673.       if (!NULL (Vquit_flag))
  674.         {
  675.           Vexecuting_macro = Qt;
  676.           QUIT;        /* Make some noise. */
  677.                 /* Will return since macro now empty. */
  678.         }
  679.     }
  680.  
  681.       /* Do redisplay processing after this command except in special
  682.      cases identified below that set no_redisplay to 1.  */
  683.       no_redisplay = 0;
  684.  
  685.       /* Execute the command.  */
  686.  
  687.       if (NULL (cmd))
  688.     {
  689.       /* nil means key is undefined.  */
  690.       bell ();
  691.       defining_kbd_macro = 0;
  692.       update_mode_lines++;
  693.       Vprefix_arg = Qnil;
  694.     }
  695.       else
  696.     {
  697.       this_command = cmd;
  698.       if (NULL (Vprefix_arg) && ! no_direct)
  699.         {
  700.           if (EQ (cmd, Qforward_char) && point < ZV)
  701.         {
  702.           lose = FETCH_CHAR (point);
  703.           SET_PT (point + 1);
  704.           if (lose >= ' ' && lose < 0177
  705.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  706.               >= MODIFF)
  707.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  708.               == point)
  709.               && !windows_or_buffers_changed
  710.               && EQ (current_buffer->selective_display, Qnil)
  711.               && !detect_input_pending ()
  712.               && NULL (Vexecuting_macro))
  713.             no_redisplay = direct_output_forward_char (1);
  714.           goto directly_done;
  715.         }
  716.           else if (EQ (cmd, Qbackward_char) && point > BEGV)
  717.         {
  718.           SET_PT (point - 1);
  719.           lose = FETCH_CHAR (point);
  720.           if (lose >= ' ' && lose < 0177
  721.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  722.               >= MODIFF)
  723.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  724.               == point)
  725.               && !windows_or_buffers_changed
  726.               && EQ (current_buffer->selective_display, Qnil)
  727.               && !detect_input_pending ()
  728.               && NULL (Vexecuting_macro))
  729.             no_redisplay = direct_output_forward_char (-1);
  730.           goto directly_done;
  731.         }
  732.           else if (EQ (cmd, Qself_insert_command))
  733.         {
  734.           if (NULL (Vexecuting_macro) &&
  735.               !EQ (minibuf_window, selected_window))
  736.             {
  737.               if (!nonundocount || nonundocount >= 20)
  738.             {
  739.               Fundo_boundary ();
  740.               nonundocount = 0;
  741.             }
  742.               nonundocount++;
  743.             }
  744.           lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
  745.               < MODIFF)
  746.             || (XFASTINT (XWINDOW (selected_window)->last_point)
  747.               != point)
  748.             || MODIFF <= current_buffer->save_modified
  749.             || windows_or_buffers_changed
  750.             || !EQ (current_buffer->selective_display, Qnil)
  751.             || detect_input_pending ()
  752.             || !NULL (Vexecuting_macro);
  753.           if (self_insert_internal (last_command_char, 0))
  754.             {
  755.               lose = 1;
  756.               nonundocount = 0;
  757.             }
  758.           if (!lose
  759.               && (point == ZV || FETCH_CHAR (point) == '\n')
  760.               && last_command_char >= ' '
  761.               && last_command_char < 0177)
  762.             no_redisplay
  763.               = direct_output_for_insert (last_command_char);
  764.           goto directly_done;
  765.         }
  766.         }
  767.  
  768.       /* Here for a command that isn't executed directly */
  769.  
  770.       nonundocount = 0;
  771.       if (NULL (Vprefix_arg))
  772.         Fundo_boundary ();
  773.       Fcommand_execute (cmd, Qnil);
  774.  
  775.     directly_done: ;
  776.     }
  777.  
  778.       if (NULL (Vprefix_arg))
  779.     {
  780.       last_command = this_command;
  781.       this_command_key_count = 0;
  782.       cancel_echoing ();
  783.     }
  784.     }
  785. }
  786.  
  787. /* Input of single characters from keyboard */
  788.  
  789. Lisp_Object print_help ();
  790.  
  791. int echo_flag;
  792. int echo_now;
  793.  
  794. /* Alarm interrupt calls this and requests echoing at earliest safe time. */
  795. request_echo ()
  796. {
  797.   int old_errno = errno;
  798.  
  799.   /* Note: no need to reestablish handler on USG systems
  800.      because it is established, if approriate, each time an alarm is requested.  */
  801. #ifdef subprocesses
  802. #ifdef BSD4_1
  803.   extern int select_alarmed;
  804.   if (select_alarmed == 0)
  805.     {
  806.       select_alarmed = 1;
  807.       sigrelse (SIGALRM);
  808.       return;
  809.     }
  810. #endif
  811. #endif
  812.  
  813. #ifdef BSD4_1
  814.   sigisheld (SIGALRM);
  815. #endif
  816.  
  817.   if (echo_now)
  818.     echo ();
  819.   else
  820.     echo_flag = 1;
  821.  
  822. #ifdef BSD4_1
  823.   sigunhold (SIGALRM);
  824. #endif
  825.  
  826.   errno = old_errno;
  827. }
  828.  
  829. /* Nonzero means polling for input is temporarily suppresed.  */
  830. int poll_suppress_count;
  831.  
  832. /* Number of seconds between polling for input.  */
  833. int polling_period;
  834.  
  835. #ifdef POLL_FOR_INPUT
  836. int polling_for_input;
  837.  
  838. /* Handle an alarm once each second and read pending input
  839.    so as to handle a C-g if it comces in.  */
  840.  
  841. input_poll_signal ()
  842. {
  843.   int junk;
  844.  
  845.   if (!waiting_for_input)
  846.     read_avail_input (&junk);
  847.   signal (SIGALRM, input_poll_signal);
  848.   alarm (polling_period);
  849. }
  850.  
  851. #endif
  852.  
  853. /* Begin signals to poll for input, if they are appropriate.
  854.    This function is called unconditionally from various places.  */
  855.  
  856. start_polling ()
  857. {
  858. #ifdef POLL_FOR_INPUT
  859.   if (read_socket_hook)
  860.     {
  861.       poll_suppress_count--;
  862.       if (poll_suppress_count == 0)
  863.     {
  864.       signal (SIGALRM, input_poll_signal);
  865.       polling_for_input = 1;
  866.       alarm (polling_period);
  867.     }
  868.     }
  869. #endif
  870. }
  871.  
  872. /* Turn off polling.  */
  873.  
  874. stop_polling ()
  875. {
  876. #ifdef POLL_FOR_INPUT
  877.   if (read_socket_hook)
  878.     {
  879.       if (poll_suppress_count == 0)
  880.     {
  881.       polling_for_input = 0;
  882.       alarm (0);
  883.     }
  884.       poll_suppress_count++;
  885.     }
  886. #endif
  887. }
  888.  
  889. /* read a character from the keyboard; call the redisplay if needed */
  890. /* commandflag 0 means do not do auto-saving, but do do redisplay.
  891.    -1 means do not do redisplay, but do do autosaving.
  892.    1 means do both.  */
  893.  
  894. read_command_char (commandflag)
  895.      int commandflag;
  896. {
  897.   register int c;
  898.   int alarmtime;
  899.   int count;
  900.   Lisp_Object tem;
  901.   jmp_buf save_jump;
  902.   extern request_echo ();
  903.  
  904.   if (unread_command_char >= 0)
  905.     {
  906.       c = unread_command_char;
  907.       unread_command_char = -1;
  908.       if (this_command_key_count == 0)
  909.     goto reread_first;
  910.       goto reread;
  911.     }
  912.  
  913.   if (!NULL (Vexecuting_macro))
  914.     {
  915.       if (XTYPE (Vexecuting_macro) != Lisp_String
  916.       || XSTRING (Vexecuting_macro)->size <= executing_macro_index)
  917.     return -1;
  918.       QUIT;
  919.       c = XSTRING (Vexecuting_macro)->data[executing_macro_index++];
  920.       goto from_macro;
  921.     }
  922.  
  923.   /* Save outer setjmp data, in case called recursively.  */
  924.   bcopy (getcjmp, save_jump, sizeof getcjmp);
  925.  
  926.   stop_polling ();
  927.  
  928.   if (commandflag >= 0 && !detect_input_pending ())
  929.     redisplay ();
  930.  
  931.   if (commandflag != 0
  932.       && auto_save_interval > 0
  933.       && num_input_chars - last_auto_save > max (auto_save_interval, 20)
  934.       && !detect_input_pending ())
  935.     Fdo_auto_save (Qnil);
  936.  
  937.   if (_setjmp (getcjmp))
  938.     {
  939.       c = quit_char;
  940.       waiting_for_input = 0;
  941.       input_available_clear_word = 0;
  942.  
  943.       goto non_reread;
  944.     }
  945.  
  946.   /* Message turns off echoing unless more keystrokes turn it on again. */
  947.   if (echo_area_contents && *echo_area_contents && echo_area_contents != echobuf)
  948.     cancel_echoing ();
  949.   else
  950.     /* If already echoing, continue, and prompt.  */
  951.     echo_dash ();
  952.  
  953.   /* If in middle of key sequence and minibuffer not active,
  954.      start echoing if enough time elapses.  */
  955.   if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
  956.       && echo_keystrokes > 0
  957.       && (echo_area_contents == 0 || *echo_area_contents == 0))
  958.     {
  959.       /* Else start echoing if user waits more than `alarmtime' seconds. */
  960.       /* This interrupt either calls echo right away
  961.      or sets echo_flag, which causes echo to be called
  962.      by set_waiting_for_input's next invocation.  */
  963.  
  964.       signal (SIGALRM, request_echo);
  965.       echo_flag = 0;
  966.       echo_now = 0;
  967.       alarmtime = echo_keystrokes;
  968.       alarm ((unsigned) alarmtime);
  969.  
  970.     }
  971.  
  972.   c = kbd_buffer_read_command_char ();
  973.  
  974.  non_reread:
  975.  
  976.   bcopy (save_jump, getcjmp, sizeof getcjmp);
  977.  
  978.   /* Cancel alarm if it was set and has not already gone off. */
  979.   if (alarmtime > 0) alarm (0);
  980.  
  981.   echo_area_contents = 0;
  982.  
  983.   if (c < 0) return -1;
  984.  
  985.   c &= meta_key ? 0377 : 0177;
  986.  
  987.   if (XTYPE (Vkeyboard_translate_table) == Lisp_String
  988.       && XSTRING (Vkeyboard_translate_table)->size > c)
  989.     c = XSTRING (Vkeyboard_translate_table)->data[c];
  990.  
  991.   total_keys++;
  992.   recent_keys[recent_keys_index] = c;
  993.   recent_keys_index = (recent_keys_index + 1) % sizeof recent_keys;
  994.  
  995.   if (dribble)
  996.     {
  997.       putc (c, dribble);
  998.       fflush (dribble);
  999.     }
  1000.  
  1001.   store_kbd_macro_char (c);
  1002.  
  1003.   start_polling ();
  1004.  
  1005.  from_macro:
  1006.  reread_first:  /* Rereading a char and it is the first in a command.  */
  1007.  
  1008.   echo_char (c);
  1009.  
  1010.   /* Record this character as part of the current key.  */
  1011.   if (this_command_key_count == this_command_keys_size)
  1012.     {
  1013.       this_command_keys_size *= 2;
  1014.       this_command_keys
  1015.     = (char *) xrealloc (this_command_keys, this_command_keys_size);
  1016.     }
  1017.   this_command_keys[this_command_key_count++] = c;
  1018.  
  1019.   /* Rereading in the middle of a command.  */
  1020.  reread:
  1021.  
  1022.   last_input_char = c;
  1023.  
  1024.   num_input_chars++;
  1025.  
  1026.   /* Process the help character specially if enabled */
  1027.   if (c == help_char && !NULL (Vhelp_form))
  1028.     {
  1029.       count = specpdl_ptr - specpdl;
  1030.  
  1031.       record_unwind_protect (Fset_window_configuration,
  1032.                  Fcurrent_window_configuration ());
  1033.  
  1034.       tem = Feval (Vhelp_form);
  1035.       if (XTYPE (tem) == Lisp_String)
  1036.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem);
  1037.  
  1038.       cancel_echoing ();
  1039.       c = read_command_char (0);
  1040.       /* Remove the help from the screen */
  1041.       unbind_to (count);
  1042.       redisplay ();
  1043.       if (c == 040)
  1044.     {
  1045.       cancel_echoing ();
  1046.       c = read_command_char (0);
  1047.     }
  1048.     }
  1049.  
  1050.   return c;
  1051. }
  1052.  
  1053. Lisp_Object
  1054. print_help (object)
  1055.      Lisp_Object object;
  1056. {
  1057.   Fprinc (object, Qnil);
  1058.   return Qnil;
  1059. }
  1060.  
  1061. /* Low level keyboard input.
  1062.  Read characters into kbd_buffer
  1063.  from which they are obtained by kbd_buffer_read_command_char.  */
  1064.  
  1065. /* Set this for debugging, to have a way to get out */
  1066. int stop_character;
  1067.  
  1068. /* Store a character obtained at interrupt level into kbd_buffer, fifo */
  1069. kbd_buffer_store_char (c)
  1070.      register int c;
  1071. {
  1072.   c &= 0377;
  1073.  
  1074.   if (c == quit_char
  1075.       || ((c == (0200 | quit_char)) && !meta_key))
  1076.     {
  1077.       interrupt_signal ();
  1078.       return;
  1079.     }
  1080.  
  1081.   if (c && c == stop_character)
  1082.     {
  1083.       sys_suspend ();
  1084.       return;
  1085.     }
  1086.  
  1087.   if (kbd_ptr != kbd_buffer)
  1088.     {
  1089.       bcopy (kbd_ptr, kbd_buffer, kbd_count);
  1090.       kbd_ptr = kbd_buffer;
  1091.     }
  1092.  
  1093.   if (kbd_count < sizeof kbd_buffer)
  1094.     {
  1095.       kbd_buffer[kbd_count++] = c;
  1096.     }
  1097. }
  1098.  
  1099. kbd_buffer_read_command_char ()
  1100. {
  1101.   register int c;
  1102.  
  1103.   if (noninteractive)
  1104.     {
  1105.       c = getchar ();
  1106.  
  1107.       return c;
  1108.     }
  1109.  
  1110.   /* Either ordinary input buffer or C-g buffered means we can return.  */
  1111.   while (!kbd_count)
  1112.     {
  1113.       if (!NULL (Vquit_flag))
  1114.     quit_throw_to_read_command_char ();
  1115.  
  1116.       /* One way or another, wait until input is available; then, if
  1117.      interrupt handlers have not read it, read it now.  */
  1118.  
  1119. #ifdef VMS
  1120.       wait_for_kbd_input ();
  1121. #else
  1122. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1123. #ifdef SIGIO
  1124.       gobble_input ();
  1125. #endif /* SIGIO */
  1126.       if (!kbd_count)
  1127.     {
  1128. #ifdef subprocesses
  1129.       wait_reading_process_input (0, -1, 1);
  1130. #else
  1131. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1132. #ifdef SIGIO
  1133.       if (interrupt_input)
  1134.         {
  1135.           sigblockx (SIGIO);
  1136.           set_waiting_for_input (0);
  1137.           while (!kbd_count)
  1138.         sigpausex (SIGIO);
  1139.           clear_waiting_for_input ();
  1140.           sigunblockx (SIGIO);
  1141.         }
  1142. #else
  1143.       interrupt_input = 0;
  1144. #endif /* not SIGIO */
  1145. #endif /* subprocesses */
  1146.  
  1147.       if (!interrupt_input && !kbd_count)
  1148.         {
  1149.           read_avail_input (0);
  1150.  
  1151.         }
  1152.     }
  1153. #endif /* not VMS */
  1154.     }
  1155.  
  1156.   input_pending = --kbd_count > 0;
  1157.   c = *kbd_ptr;            /* *kbd_ptr++ would have a timing error. */
  1158.   kbd_ptr++;            /* See kbd_buffer_store_char. */
  1159.   return (c & (meta_key ? 0377 : 0177)); /* Clean up if sign was extended. */
  1160. }
  1161.  
  1162. /* Force an attempt to read input regardless of what FIONREAD says.  */
  1163.  
  1164. force_input_read ()
  1165. {
  1166.   force_input = 1;
  1167.   detect_input_pending ();
  1168.   force_input = 0;
  1169. }
  1170.  
  1171. /* Store into *addr the number of terminal input chars available.
  1172.    Equivalent to ioctl (0, FIONREAD, addr) but works
  1173.    even if FIONREAD does not exist.  */
  1174.  
  1175. static void
  1176. get_input_pending (addr)
  1177.      int *addr;
  1178. {
  1179. #ifdef VMS
  1180.   /* On VMS, we always have something in the buffer
  1181.      if any input is available.  */
  1182.   /*** It might be simpler to make interrupt_input 1 on VMS ***/
  1183.   *addr = kbd_count | !NULL (Vquit_flag);
  1184. #else
  1185.   /* First of all, have we already counted some input?  */
  1186.   *addr = kbd_count | !NULL (Vquit_flag);
  1187.   /* If input is being read as it arrives, and we have none, there is none.  */
  1188.   if (*addr > 0 || (interrupt_input && ! interrupts_deferred && ! force_input))
  1189.     return;
  1190. #ifdef FIONREAD
  1191.   if (! force_input)
  1192.     {
  1193.       /* If we can count the input without reading it, do so.  */
  1194.       if (ioctl (0, FIONREAD, addr) < 0)
  1195.     *addr = 0;
  1196.       if (*addr == 0 || read_socket_hook == 0)
  1197.     return;
  1198.       /* If the input consists of window-events, not all of them
  1199.      are necessarily kbd chars.  So process all the input
  1200.      and see how many kbd chars we got.  */
  1201.     }
  1202. #endif
  1203. #ifdef SIGIO
  1204.   {
  1205.     /* It seems there is a timing error such that a SIGIO can be handled here
  1206.        and cause kbd_count to become nonzero even though raising of SIGIO
  1207.        has already been turned off.  */
  1208.     int mask = sigblock (sigmask (SIGIO));
  1209.     if (kbd_count == 0)
  1210.       read_avail_input (*addr);
  1211.     sigsetmask (mask);
  1212.   }
  1213. #else
  1214.   /* If we can't count the input, read it (if any) and see what we got.  */
  1215.   read_avail_input (*addr);
  1216. #endif
  1217.   *addr = kbd_count | !NULL (Vquit_flag);
  1218. #endif
  1219. }
  1220.  
  1221. /* Read pending any input out of the system and into Emacs.  */
  1222.  
  1223. /* This function is temporary in Emacs 18.  It is used only
  1224.    with X windows.  X windows always turns on interrupt input
  1225.    if possible, so this function has nothing to do except
  1226.    on systems that don't have SIGIO.  And they also don't have FIONREAD.  */
  1227. void
  1228. consume_available_input ()
  1229. {
  1230. #ifdef SIGIO
  1231.   if (!interrupt_input || interrupts_deferred)
  1232. #endif
  1233.     read_avail_input (0);
  1234. }
  1235.  
  1236. /* Read any terminal input already buffered up by the system
  1237.    into the kbd_buffer, assuming the buffer is currently empty.
  1238.    Never waits.
  1239.  
  1240.    If NREAD is nonzero, assume it contains # chars of raw data waiting.
  1241.    If it is zero, we determine that datum.
  1242.  
  1243.    Input gets into the kbd_buffer either through this function
  1244.    (at main program level) or at interrupt level if input
  1245.    is interrupt-driven.  */
  1246.  
  1247. static void
  1248. read_avail_input (nread)
  1249.      int nread;
  1250. {
  1251.   /* This function is not used on VMS.  */
  1252. #ifndef VMS
  1253.   char buf[256 * BUFFER_SIZE_FACTOR];
  1254.   register int i;
  1255.  
  1256. #ifdef FIONREAD
  1257.   if (! force_input)
  1258.     {
  1259.       if (nread == 0)
  1260.     get_input_pending (&nread);
  1261.       if (nread == 0)
  1262.     return;
  1263.     }
  1264.   if (nread > sizeof buf)
  1265.     nread = sizeof buf;
  1266.  
  1267.   /* Read what is waiting.  */
  1268.   if (read_socket_hook)
  1269.     nread = (*read_socket_hook) (0, buf, nread);
  1270.   else
  1271.     nread = read (0, buf, nread);
  1272.  
  1273. #else /* no FIONREAD */
  1274. /**
  1275.  **  (sjk)++ changed next line from "ifdef USG" to "if defined(USG)"
  1276.  **/
  1277. #if defined(USG)
  1278.   fcntl (fileno (stdin), F_SETFL, O_NDELAY);
  1279.   if (read_socket_hook)
  1280.     {
  1281.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1282.     }
  1283.   else
  1284.     {
  1285.       nread = read (fileno (stdin), buf, sizeof buf);
  1286.     }
  1287. #ifdef EBADSLT
  1288.   if (nread == -1 && (errno == EAGAIN || errno == EBADSLT))
  1289. #else
  1290.   if (nread == -1 && errno == EAGAIN)
  1291. #endif
  1292.     nread = 0;
  1293.   fcntl (fileno (stdin), F_SETFL, 0);
  1294. #else /* not USG */
  1295.   you lose
  1296. #endif /* not USG */
  1297. #endif /* no FIONREAD */
  1298.  
  1299.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  1300.   if (kbd_count == 0)
  1301.     kbd_ptr = kbd_buffer;
  1302.  
  1303.   for (i = 0; i < nread; i++)
  1304.     {
  1305.       kbd_buffer_store_char (buf[i]);
  1306.       /* Don't look at input that follows a C-g too closely.
  1307.      This reduces lossage due to autorepeat on C-g.  */
  1308.       if (buf[i] == quit_char)
  1309.     break;
  1310.     }
  1311. #endif /* not VMS */
  1312. }
  1313.  
  1314. #ifdef SIGIO   /* for entire page */
  1315. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1316.  
  1317. /* If using interrupt input and some input chars snuck into the
  1318.    buffer before we enabled interrupts, fake an interrupt for them.  */
  1319.  
  1320. gobble_input ()
  1321. {
  1322.   int nread;
  1323.   if (interrupt_input)
  1324.     {
  1325.       if (ioctl (0, FIONREAD, &nread) < 0)
  1326.     nread = 0;
  1327.       if (nread)
  1328.     {
  1329.       sigholdx (SIGIO);
  1330.       input_available_signal (SIGIO);
  1331.       sigfree ();
  1332.     }
  1333.     }
  1334. }
  1335.  
  1336. input_available_signal (signo)
  1337.      int signo;
  1338. {
  1339.   unsigned char buf[256 * BUFFER_SIZE_FACTOR];
  1340.   int nread;
  1341.   register int i;
  1342.   /* Must preserve main program's value of errno.  */
  1343.   int old_errno = errno;
  1344. #ifdef BSD4_1
  1345.   extern int select_alarmed;
  1346. #endif
  1347.  
  1348. #ifdef USG
  1349.   /* USG systems forget handlers when they are used;
  1350.      must reestablish each time */
  1351.   signal (signo, input_available_signal);
  1352. #endif /* USG */
  1353.  
  1354. #ifdef BSD4_1
  1355.   sigisheld (SIGIO);
  1356. #endif
  1357.  
  1358.   if (input_available_clear_word)
  1359.     *input_available_clear_word = 0;
  1360.  
  1361.   while (1)
  1362.     {
  1363.       if (ioctl (0, FIONREAD, &nread) < 0)
  1364.     /* Formerly simply exited the loop, but that sometimes led to
  1365.        a failure of Emacs to terminate.
  1366.        SIGHUP seems appropriate if we can't reach the terminal.  */
  1367.     kill (getpid (), SIGHUP);
  1368.       if (nread <= 0)
  1369.     break;
  1370. #ifdef BSD4_1
  1371.       select_alarmed = 1;  /* Force the select emulator back to life */
  1372. #endif
  1373.       if (read_socket_hook)
  1374.     {
  1375.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1376.       if (!nread)
  1377.         continue;
  1378.     }
  1379.       else
  1380.     {
  1381.       if (nread > sizeof buf)
  1382.         nread = sizeof buf;
  1383.       nread = read (0, buf, nread);
  1384.     }
  1385.  
  1386.       for (i = 0; i < nread; i++)
  1387.     {
  1388.       kbd_buffer_store_char (buf[i]);
  1389.       /* Don't look at input that follows a C-g too closely.
  1390.          This reduces lossage due to autorepeat on C-g.  */
  1391.       if (buf[i] == quit_char)
  1392.         break;
  1393.     }
  1394.     }
  1395. #ifdef BSD4_1
  1396.   sigfree ();
  1397. #endif
  1398.   errno = old_errno;
  1399. }
  1400. #endif /* SIGIO */
  1401.  
  1402. #if 0
  1403. /* This is turned off because it didn't produce much speedup.  */
  1404.  
  1405. /* Read a single-char key sequence.  Do not redisplay.
  1406.    Return 1 if successful, or 0 if what follows is not
  1407.    a single-char key.  (In that case, a char has been unread.)
  1408.    This is used instead of read_key_sequence as an optimization
  1409.    just after a direct-updating command is done, since at such
  1410.    times we know that no redisplay is required.  */
  1411.  
  1412. int
  1413. fast_read_one_key (keybuf)
  1414.      char *keybuf;
  1415. {
  1416.   register Lisp_Object map;
  1417.   register int c;
  1418.   register Lisp_Object tem;
  1419.  
  1420.   keys_prompt = 0;
  1421.   /* Read a character, and do not redisplay.  */
  1422.   c = read_command_char (-1);
  1423.   Vquit_flag = Qnil;
  1424.  
  1425.   /* Assume until further notice that we are unlucky
  1426.      and will return zero, so this char will be
  1427.      reread by read_key_sequence.  */
  1428.  
  1429.   unread_command_char = c;
  1430.  
  1431.   if (c < 0 || c >= 0200)
  1432.     return 0;
  1433.  
  1434.   map = current_buffer->keymap;
  1435.   if (!EQ (map, Qnil))
  1436.     {
  1437.       tem = get_keyelt (access_keymap (map, c));
  1438.       if (!EQ (tem, Qnil))
  1439.     return 0;
  1440.     }
  1441.  
  1442.   XSET (map, Lisp_Vector, global_map);
  1443.   tem = !NULL (map)
  1444.     ? get_keyelt (access_keymap (map, c))
  1445.       : Qnil;
  1446.  
  1447.   read_key_sequence_cmd = tem;
  1448.  
  1449.   /* trace symbols to their function definitions */
  1450.  
  1451.   while (XTYPE (tem) == Lisp_Symbol && !NULL (tem)
  1452.      && !EQ (tem, Qunbound))
  1453.     tem = XSYMBOL (tem)->function;
  1454.  
  1455.   /* Is the definition a prefix character?  */
  1456.  
  1457.   if (XTYPE (tem) == Lisp_Vector ||
  1458.       (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap)))
  1459.     return 0;
  1460.  
  1461.   unread_command_char = -1;
  1462.   keybuf[0] = c;
  1463.   return 1;
  1464. }
  1465.  
  1466. #endif /* 0 */
  1467.  
  1468. /* Read a sequence of keys that ends with a non prefix character,
  1469.  and store them in KEYBUF, a buffer of size BUFSIZE.
  1470.  Prompt with PROMPT.  Echo starting immediately unless `prompt' is 0.
  1471.  Return the length of the key sequence stored.
  1472.  NODISPLAY nonzero means don't do redisplay before the first character
  1473.  (just for speedup).  */
  1474.  
  1475. int
  1476. read_key_sequence (keybuf, bufsize, prompt, nodisplay)
  1477.      char *keybuf;
  1478.      int bufsize;
  1479.      unsigned char *prompt;
  1480.      int nodisplay;
  1481. {
  1482.   register int i;
  1483.   Lisp_Object nextlocal, nextglobal;
  1484.   register int c, nextc;
  1485.   Lisp_Object local, global;
  1486.  
  1487.   if (FROM_KBD)
  1488.     {
  1489.       if (prompt)
  1490.     echo_prompt (prompt);
  1491.       else if (cursor_in_echo_area)
  1492.     echo_dash ();
  1493.     }
  1494.  
  1495.   nextc = read_command_char (nodisplay ? -1 : !prompt);
  1496.   nextlocal = current_buffer->keymap;
  1497.   XSET (nextglobal, Lisp_Vector, global_map);
  1498.  
  1499.   i = 0;
  1500.   while (!NULL (nextlocal) || !NULL (nextglobal))
  1501.     {
  1502.       if (i == bufsize)
  1503.     error ("key sequence too long");
  1504.  
  1505.       if (nextc >= 0)
  1506.     {
  1507.       c = nextc;
  1508.       nextc = -1;
  1509.     }
  1510.       else
  1511.     c = read_command_char (!prompt);
  1512.       Vquit_flag = Qnil;
  1513.       nodisplay = 0;
  1514.  
  1515.       if (c < 0)
  1516.     return 0;
  1517.       if (c >= 0200)
  1518.     {
  1519.       nextc = c & 0177;
  1520.       c = meta_prefix_char;
  1521.     }
  1522.  
  1523.       keybuf[i] = c;
  1524.  
  1525.       global = !NULL (nextglobal)
  1526.     ? get_keyelt (access_keymap (nextglobal, c))
  1527.       : Qnil;
  1528.  
  1529.       local = !NULL (nextlocal)
  1530.     ? get_keyelt (access_keymap (nextlocal, c))
  1531.       : Qnil;
  1532.  
  1533.       /* If C is not defined in either keymap
  1534.      and it is an uppercase letter, try corresponding lowercase.  */
  1535.  
  1536.       if (NULL (global) && NULL (local) && UPPERCASEP (c))
  1537.     {
  1538.       global = !NULL (nextglobal)
  1539.         ? get_keyelt (access_keymap (nextglobal, DOWNCASE (c)))
  1540.           : Qnil;
  1541.  
  1542.       local = !NULL (nextlocal)
  1543.         ? get_keyelt (access_keymap (nextlocal, DOWNCASE (c)))
  1544.           : Qnil;
  1545.  
  1546.       /* If that has worked better that the original char,
  1547.          downcase it permanently.  */
  1548.  
  1549.       if (!NULL (global) || !NULL (local))
  1550.         {
  1551.           keybuf[i] = c = DOWNCASE (c);
  1552.         }
  1553.     }
  1554.  
  1555.       i++;
  1556.  
  1557.       nextlocal = Qnil;
  1558.       nextglobal = Qnil;
  1559.  
  1560.       read_key_sequence_cmd = !NULL (local) ? local : global;
  1561.  
  1562.       /* trace symbols to their function definitions */
  1563.  
  1564.       while (XTYPE (global) == Lisp_Symbol && !NULL (global)
  1565.          && !EQ (global, Qunbound))
  1566.     global = XSYMBOL (global)->function;
  1567.       while (XTYPE (local) == Lisp_Symbol && !NULL (local)
  1568.          && !EQ (local, Qunbound))
  1569.     local = XSYMBOL (local)->function;
  1570.  
  1571.       /* Are the definitions prefix characters? */
  1572.  
  1573.       if (XTYPE (local) == Lisp_Vector ||
  1574.       (CONSP (local) && EQ (XCONS (local)->car, Qkeymap))
  1575.       ||
  1576.       /* If nextc is set, we are processing a prefix char
  1577.          that represents a meta-bit.
  1578.          Let a global prefix definition override a local non-prefix.
  1579.          This is for minibuffers that redefine Escape for completion.
  1580.          A real Escape gets completion, but Meta bits get ESC-prefix.  */
  1581.       ((NULL (local) || nextc >= 0)
  1582.        && (XTYPE (global) == Lisp_Vector ||
  1583.            (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))))
  1584.     {
  1585.       if (XTYPE (local) == Lisp_Vector ||
  1586.           (CONSP (local) && EQ (XCONS (local)->car, Qkeymap)))
  1587.         nextlocal = local;
  1588.       else
  1589.         nextlocal = Qnil;
  1590.  
  1591.       if (XTYPE (global) == Lisp_Vector ||
  1592.           (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))
  1593.         nextglobal = global;
  1594.       else
  1595.         nextglobal = Qnil;
  1596.     }
  1597.     }
  1598.  
  1599.   return i;
  1600. }
  1601.  
  1602. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
  1603.   "Read a sequence of keystrokes and return as a string.\n\
  1604. The sequence is sufficient to specify a non-prefix command\n\
  1605. starting from the current local and global keymaps.\n\
  1606. A C-g typed while in this function is treated like\n\
  1607. any other character, and quit-flag is not set.\n\
  1608. One arg, PROMPT, a prompt string or  nil, meaning do not prompt specially.")
  1609.   (prompt)
  1610.      Lisp_Object prompt;
  1611. {
  1612.   char keybuf[30];
  1613.   register int i;
  1614.  
  1615.   if (!NULL (prompt))
  1616.     CHECK_STRING (prompt, 0);
  1617.   QUIT;
  1618.  
  1619.   this_command_key_count = 0;
  1620.   i = read_key_sequence (keybuf, sizeof keybuf,
  1621.              (NULL (prompt)) ? 0 : XSTRING (prompt)->data,
  1622.              0);
  1623.   return make_string (keybuf, i);
  1624. }
  1625.  
  1626. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
  1627.  "Execute CMD as an editor command.\n\
  1628. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  1629. Optional second arg RECORD-FLAG non-nil\n\
  1630. means unconditionally put this command in the command-history.\n\
  1631. Otherwise, this is done only if an arg is read using the minibuffer.")
  1632.      (cmd, record)
  1633.      Lisp_Object cmd, record;
  1634. {
  1635.   register Lisp_Object final;
  1636.   register Lisp_Object tem;
  1637.   Lisp_Object prefixarg;
  1638.   struct backtrace backtrace;
  1639.   extern int debug_on_next_call;
  1640.  
  1641.   prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
  1642.   Vcurrent_prefix_arg = prefixarg;
  1643.   debug_on_next_call = 0;
  1644.  
  1645.   if (XTYPE (cmd) == Lisp_Symbol)
  1646.     {
  1647.       tem = Fget (cmd, Qdisabled);
  1648.       if (!NULL (tem))
  1649.     return call0 (Vdisabled_command_hook);
  1650.     }
  1651.  
  1652.   while (1)
  1653.     {
  1654.       final = cmd;
  1655.       while (XTYPE (final) == Lisp_Symbol)
  1656.     {
  1657.       if (EQ (Qunbound, XSYMBOL (final)->function))
  1658.         Fsymbol_function (final);    /* Get an error! */
  1659.       final = XSYMBOL (final)->function;
  1660.     }
  1661.  
  1662.       if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
  1663.     do_autoload (final, cmd);
  1664.       else
  1665.     break;
  1666.     }
  1667.  
  1668.   if (CONSP (final) || XTYPE (final) == Lisp_Subr)
  1669.     {
  1670.       backtrace.next = backtrace_list;
  1671.       backtrace_list = &backtrace;
  1672.       backtrace.function = &Qcall_interactively;
  1673.       backtrace.args = &cmd;
  1674.       backtrace.nargs = 1;
  1675.       backtrace.evalargs = 0;
  1676.  
  1677.       tem = Fcall_interactively (cmd, record);
  1678.  
  1679.       backtrace_list = backtrace.next;
  1680.       return tem;
  1681.     }
  1682.   if (XTYPE (final) == Lisp_String)
  1683.     {
  1684.       return Fexecute_kbd_macro (final, prefixarg);
  1685.     }
  1686.   return Qnil;
  1687. }
  1688.  
  1689. DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
  1690.   1, 1, "P",
  1691.   "Read function name, then read its arguments and call it.")
  1692.   (prefixarg)
  1693.      Lisp_Object prefixarg;
  1694. {
  1695.   Lisp_Object function;
  1696.   char buf[40];
  1697.   Lisp_Object saved_keys;
  1698.   struct gcpro gcpro1;
  1699.  
  1700.   saved_keys = Fthis_command_keys ();
  1701.   GCPRO1 (saved_keys);
  1702.  
  1703.   buf[0] = 0;
  1704.  
  1705.   if (EQ (prefixarg, Qminus))
  1706.     strcpy (buf, "- ");
  1707.   else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
  1708.     strcpy (buf, "C-u ");
  1709.   else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
  1710.     sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
  1711.   else if (XTYPE (prefixarg) == Lisp_Int)
  1712.     sprintf (buf, "%d ", XINT (prefixarg));
  1713.  
  1714.   /* This isn't strictly correct if execute-extended-command
  1715.      is bound to anything else */
  1716.   strcat (buf, "M-x ");
  1717.  
  1718.   function = Fcompleting_read (build_string (buf), Vobarray, Qcommandp, Qt, Qnil);
  1719.  
  1720.   saved_keys = concat2 (saved_keys, function);
  1721.   if (this_command_keys_size < XSTRING (function)->size)
  1722.     {
  1723.       this_command_keys_size += XSTRING (function)->size;
  1724.       this_command_keys = (char *) xrealloc (this_command_keys,
  1725.                           this_command_keys_size);
  1726.     }
  1727.   bcopy (XSTRING (function)->data, this_command_keys,
  1728.      XSTRING (function)->size + 1);
  1729.   this_command_key_count = XSTRING (saved_keys)->size;
  1730.  
  1731.   UNGCPRO;
  1732.  
  1733.   function = Fintern (function, Vobarray);
  1734.   Vprefix_arg = prefixarg;
  1735.   this_command = function;
  1736.  
  1737.   return Fcommand_execute (function, Qt);
  1738. }
  1739.  
  1740. detect_input_pending ()
  1741. {
  1742.   if (!input_pending)
  1743.     get_input_pending (&input_pending);
  1744.  
  1745.   return input_pending;
  1746. }
  1747.  
  1748. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  1749.   "T if command input is currently available with no waiting.\n\
  1750. Actually, the value is NIL only if we can be sure that no input is available.")
  1751.   ()
  1752. {
  1753.   if (unread_command_char >= 0) return Qt;
  1754.  
  1755.   return detect_input_pending () ? Qt : Qnil;
  1756. }
  1757.  
  1758. DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
  1759.   "Return string of last 100 chars read from terminal.")
  1760.   ()
  1761. {
  1762.   Lisp_Object val;
  1763.   if (total_keys < sizeof recent_keys)
  1764.     return make_string (recent_keys, total_keys);
  1765.  
  1766.   val = make_string (recent_keys, sizeof recent_keys);
  1767.   bcopy (recent_keys + recent_keys_index,
  1768.      XSTRING (val)->data,
  1769.      sizeof recent_keys - recent_keys_index);
  1770.   bcopy (recent_keys,
  1771.      XSTRING (val)->data + sizeof recent_keys - recent_keys_index,
  1772.      recent_keys_index);
  1773.   return val;
  1774. }
  1775.  
  1776. DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
  1777.   "Return string of the keystrokes that invoked this command.")
  1778.   ()
  1779. {
  1780.   return make_string (this_command_keys, this_command_key_count);
  1781. }
  1782.  
  1783. DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
  1784.   "Return the current depth in recursive edits.")
  1785.   ()
  1786. {
  1787.   Lisp_Object temp;
  1788.   XFASTINT (temp) = command_loop_level + minibuf_level;
  1789.   return temp;
  1790. }
  1791.  
  1792. DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
  1793.   "FOpen dribble file: ",
  1794.   "Start writing all keyboard characters to FILE.")
  1795.   (file)
  1796.      Lisp_Object file;
  1797. {
  1798.   file = Fexpand_file_name (file, Qnil);
  1799. /**
  1800.  **  (sjk)++ typecast to keep gcc quiet.
  1801.  **/
  1802. #if defined(atarist)
  1803.   dribble = fopen ((char *)XSTRING (file)->data, "w");
  1804. #else
  1805.   dribble = fopen (XSTRING (file)->data, "w");
  1806. #endif
  1807.  
  1808.   return Qnil;
  1809. }
  1810.  
  1811. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  1812.   "Discard the contents of the terminal input buffer.\n\
  1813. Also flush any kbd macro definition in progress.")
  1814.   ()
  1815. {
  1816.   defining_kbd_macro = 0;
  1817.   update_mode_lines++;
  1818.  
  1819.   unread_command_char = -1;
  1820.   discard_tty_input ();
  1821.  
  1822.   kbd_count = 0;
  1823.   input_pending = 0;
  1824.  
  1825.   return Qnil;
  1826. }
  1827.  
  1828. DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
  1829.   "Stop Emacs and return to superior process.  You can resume.\n\
  1830. If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
  1831. to be read as terminal input by Emacs's superior shell.\n\
  1832. Before suspending, if `suspend-hook' is bound and value is non-nil\n\
  1833. call the value as a function of no args.  Don't suspend if it returns non-nil.\n\
  1834. Otherwise, suspend normally and after resumption call\n\
  1835. `suspend-resume-hook' if that is bound and non-nil.")
  1836.   (stuffstring)
  1837.      Lisp_Object stuffstring;
  1838. {
  1839.   register Lisp_Object tem;
  1840.   int count = specpdl_ptr - specpdl;
  1841.   int old_height, old_width;
  1842.   int width, height;
  1843.   struct gcpro gcpro1;
  1844.   extern init_sys_modes ();
  1845.  
  1846.   if (!NULL (stuffstring))
  1847.     CHECK_STRING (stuffstring, 0);
  1848.   GCPRO1 (stuffstring);
  1849.  
  1850.   /* Call value of suspend-hook
  1851.      if it is bound and value is non-nil.  */
  1852.   tem = intern ("suspend-hook");
  1853.   tem = XSYMBOL (tem)->value;
  1854.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1855.     {
  1856.       tem = call0 (tem);
  1857.       if (!EQ (tem, Qnil)) return Qnil;
  1858.     }
  1859.  
  1860.   get_screen_size (&old_width, &old_height);
  1861.   reset_sys_modes ();
  1862.   /* sys_suspend can get an error if it tries to fork a subshell
  1863.      and the system resources aren't available for that.  */
  1864.   record_unwind_protect (init_sys_modes, 0);
  1865.   stuff_buffered_input (stuffstring);
  1866.   sys_suspend ();
  1867.   unbind_to (count);
  1868.  
  1869.   /* Check if terminal/window size has changed.
  1870.      Note that this is not useful when we are running directly
  1871.      with a window system; but suspend should be disabled in that case.  */
  1872.   get_screen_size (&width, &height);
  1873.   if (width != old_width || height != old_height)
  1874.     change_screen_size (height, width, 0);
  1875.  
  1876.   /* Call value of suspend-resume-hook
  1877.      if it is bound and value is non-nil.  */
  1878.   tem = intern ("suspend-resume-hook");
  1879.   tem = XSYMBOL (tem)->value;
  1880.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1881.     call0 (tem);
  1882.   UNGCPRO;
  1883.   return Qnil;
  1884. }
  1885.  
  1886. /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
  1887.    Then in any case stuff anthing Emacs has read ahead and not used.  */
  1888.  
  1889. stuff_buffered_input (stuffstring)
  1890.      Lisp_Object stuffstring;
  1891. {
  1892.   register unsigned char *p;
  1893.  
  1894. /* stuff_char works only in BSD, versions 4.2 and up.  */
  1895. #ifdef BSD
  1896. #ifndef BSD4_1
  1897.   if (XTYPE (stuffstring) == Lisp_String)
  1898.     {
  1899.       register int count;
  1900.  
  1901.       p = XSTRING (stuffstring)->data;
  1902.       count = XSTRING (stuffstring)->size;
  1903.       while (count-- > 0)
  1904.     stuff_char (*p++);
  1905.       stuff_char ('\n');
  1906.     }
  1907.   /* Anything we have read ahead, put back for the shell to read.  */
  1908.   while (kbd_count)
  1909.     {
  1910.       stuff_char (*kbd_ptr++);
  1911.       kbd_count--;
  1912.     }
  1913.   input_pending = 0;
  1914. #endif
  1915. #endif /* BSD and not BSD4_1 */
  1916. }
  1917.  
  1918. set_waiting_for_input (word_to_clear)
  1919.      long *word_to_clear;
  1920. {
  1921.   input_available_clear_word = word_to_clear;
  1922.  
  1923.   /* Tell interrupt_signal to throw back to read_command_char,  */
  1924.   waiting_for_input = 1;
  1925.  
  1926.   /* If interrupt_signal was called before and buffered a C-g,
  1927.      make it run again now, to avoid timing error.  */
  1928.   if (!NULL (Vquit_flag))
  1929.     quit_throw_to_read_command_char ();
  1930.  
  1931.   /* Tell alarm signal to echo right away */
  1932.   echo_now = 1;
  1933.  
  1934.   /* If alarm has gone off already, echo now.  */
  1935.   if (echo_flag)
  1936.     {
  1937.       echo ();
  1938.       echo_flag = 0;
  1939.     }
  1940. }
  1941.  
  1942. clear_waiting_for_input ()
  1943. {
  1944.   /* Tell interrupt_signal not to throw back to read_command_char,  */
  1945.   waiting_for_input = 0;
  1946.   echo_now = 0;
  1947.   input_available_clear_word = 0;
  1948. }
  1949.  
  1950. /* This routine is called at interrupt level in response to C-G.
  1951.  If interrupt_input, this is the handler for SIGINT.
  1952.  Otherwise, it is called from kbd_buffer_store_char,
  1953.  in handling SIGIO or SIGTINT.
  1954.  
  1955.  If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
  1956.  immediately throw back to read_command_char.
  1957.  
  1958.  Otherwise it sets the Lisp variable  quit-flag  not-nil.
  1959.  This causes  eval  to throw, when it gets a chance.
  1960.  If  quit-flag  is already non-nil, it stops the job right away.  */
  1961.  
  1962. interrupt_signal ()
  1963. {
  1964.   char c;
  1965.   /* Must preserve main program's value of errno.  */
  1966.   int old_errno = errno;
  1967.   extern Lisp_Object Vwindow_system;
  1968.  
  1969. #ifdef USG
  1970.   /* USG systems forget handlers when they are used;
  1971.      must reestablish each time */
  1972.   signal (SIGINT, interrupt_signal);
  1973.   signal (SIGQUIT, interrupt_signal);
  1974. #endif /* USG */
  1975.  
  1976.   cancel_echoing ();
  1977.  
  1978.   if (!NULL (Vquit_flag) && NULL (Vwindow_system))
  1979.     {
  1980.       fflush (stdout);
  1981.       reset_sys_modes ();
  1982.       sigfree ();
  1983. #ifdef SIGTSTP            /* Support possible in later USG versions */
  1984. /*
  1985.  * On systems which can suspend the current process and return to the original
  1986.  * shell, this command causes the user to end up back at the shell.
  1987.  * The "Auto-save" and "Abort" questions are not asked until
  1988.  * the user elects to return to emacs, at which point he can save the current
  1989.  * job and either dump core or continue.
  1990.  */
  1991.       sys_suspend ();
  1992. #else
  1993. #ifdef VMS
  1994.       if (sys_suspend () == -1)
  1995.     {
  1996.       printf ("Not running as a subprocess;\n");
  1997.       printf ("you can continue or abort.\n");
  1998.     }
  1999. #else /* not VMS */
  2000.       /* Perhaps should really fork an inferior shell?
  2001.      But that would not provide any way to get back
  2002.      to the original shell, ever.  */
  2003.       printf ("No support for stopping a process on this operating system;\n");
  2004.       printf ("you can continue or abort.\n");
  2005. #endif /* not VMS */
  2006. #endif /* not SIGTSTP */
  2007.       printf ("Auto-save? (y or n) ");
  2008.       fflush (stdout);
  2009.       if (((c = getchar ()) & ~040) == 'Y')
  2010.     Fdo_auto_save (Qnil);
  2011.       while (c != '\n') c = getchar ();
  2012. #ifdef VMS
  2013.       printf ("Abort (and enter debugger)? (y or n) ");
  2014. #else /* not VMS */
  2015.       printf ("Abort (and dump core)? (y or n) ");
  2016. #endif /* not VMS */
  2017.       fflush (stdout);
  2018.       if (((c = getchar ()) & ~040) == 'Y')
  2019.  
  2020.     abort ();
  2021.       while (c != '\n') c = getchar ();
  2022.  
  2023.       printf ("Continuing...\n");
  2024.       fflush (stdout);
  2025.       init_sys_modes ();
  2026.     }
  2027.   else
  2028.     {
  2029.       /* If executing a function that wants to be interrupted out of
  2030.          and the user has not deferred quitting by binding `inhibit-quit'
  2031.          then quit right away.  */
  2032.       if (immediate_quit && NULL (Vinhibit_quit))
  2033.     {
  2034.       immediate_quit = 0;
  2035.           sigfree ();
  2036.       Fsignal (Qquit, Qnil);
  2037.     }
  2038.       else
  2039.     /* Else request quit when it's safe */
  2040.     Vquit_flag = Qt;
  2041.     }
  2042.  
  2043.   if (waiting_for_input && !echoing)
  2044.     quit_throw_to_read_command_char ();
  2045.  
  2046.   errno = old_errno;
  2047. }
  2048.  
  2049. /* Handle a C-g by making read_command_char return C-g.  */
  2050.  
  2051. quit_throw_to_read_command_char ()
  2052. {
  2053.   quit_error_check ();
  2054.   sigfree ();
  2055.   /* Prevent another signal from doing this before we finish.  */
  2056.   waiting_for_input = 0;
  2057.   input_pending = 0;
  2058.   unread_command_char = -1;
  2059. #ifdef POLL_FOR_INPUT
  2060.   if (poll_suppress_count != 1)
  2061.     abort ();
  2062. #endif
  2063.   _longjmp (getcjmp, 1);
  2064. }
  2065.  
  2066. DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 2, 3, 0,
  2067.   "Set mode of reading keyboard input.\n\
  2068. First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
  2069. Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
  2070.  (no effect except in CBREAK mode).\n\
  2071. Optional third arg non-nil specifies character to use for quitting.\n\n\
  2072. Note that the arguments will change incompatibly in version 19.")
  2073.   (interrupt, flow, quit)
  2074.      Lisp_Object interrupt, flow, quit;
  2075. {
  2076.   reset_sys_modes ();
  2077. #ifdef SIGIO
  2078. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2079. #ifdef NO_SOCK_SIGIO
  2080.   if (read_socket_hook)
  2081.     interrupt_input = 0;    /* No interrupts if reading from a socket.  */
  2082.   else
  2083. #endif /* NO_SOCK_SIGIO */
  2084.     interrupt_input = !NULL (interrupt);
  2085. #else /* not SIGIO */
  2086.   interrupt_input = 0;
  2087. #endif /* not SIGIO */
  2088.   flow_control = !NULL (flow);
  2089.   if (!NULL (quit))
  2090.     {
  2091.       CHECK_NUMBER (quit, 2);
  2092.       quit_char = XINT (quit);
  2093.       /* Don't let this value be out of range.  */
  2094.       quit_char &= (meta_key ? 0377 : 0177);
  2095.     }
  2096.   init_sys_modes ();
  2097.   return Qnil;
  2098. }
  2099.  
  2100. init_keyboard ()
  2101. {
  2102.   this_command_keys_size = 40;
  2103.   this_command_keys = (char *) xmalloc (40);
  2104.  
  2105.   command_loop_level = -1;    /* Correct, before outermost invocation.  */
  2106.   quit_char = Ctl ('G');
  2107.   immediate_quit = 0;
  2108.   unread_command_char = -1;
  2109.   recent_keys_index = 0;
  2110.   total_keys = 0;
  2111.   kbd_count = 0;
  2112.   kbd_ptr = kbd_buffer;
  2113.   input_pending = 0;
  2114.   force_input = 0;
  2115.   if (!noninteractive)
  2116.     {
  2117. /**
  2118.  **  (sjk)++ another typecast.
  2119.  **/
  2120. #if defined(atarist)
  2121.       signal (SIGINT, (__Sigfunc)interrupt_signal);
  2122. #else
  2123.       signal (SIGINT, interrupt_signal);
  2124. #endif
  2125.  
  2126. #ifdef USG
  2127.       /* On USG systems, C-g is set up for both SIGINT and SIGQUIT
  2128.      and we can't tell which one it will give us.  */
  2129.       signal (SIGQUIT, interrupt_signal);
  2130. #endif /* USG */
  2131. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2132. #ifdef SIGIO
  2133.       signal (SIGIO, input_available_signal);
  2134. #endif SIGIO
  2135.     }
  2136.  
  2137. /* Use interrupt input by default, if it works and noninterrupt input
  2138.    has deficiencies.  */
  2139.  
  2140. #ifdef INTERRUPT_INPUT
  2141.   interrupt_input = 1;
  2142. #else
  2143.   interrupt_input = 0;
  2144. #endif
  2145.  
  2146.   sigfree ();
  2147.   dribble = 0;
  2148.  
  2149.   if (keyboard_init_hook)
  2150.     (*keyboard_init_hook) ();
  2151.  
  2152.   poll_suppress_count = 1;
  2153. #ifdef POLL_FOR_INPUT
  2154.   start_polling ();
  2155. #endif
  2156. }
  2157.  
  2158. syms_of_keyboard ()
  2159. {
  2160.   Qself_insert_command = intern ("self-insert-command");
  2161.   staticpro (&Qself_insert_command);
  2162.  
  2163.   Qforward_char = intern ("forward-char");
  2164.   staticpro (&Qforward_char);
  2165.  
  2166.   Qbackward_char = intern ("backward-char");
  2167.   staticpro (&Qbackward_char);
  2168.  
  2169.   Qtop_level = intern ("top-level");
  2170.   staticpro (&Qtop_level);
  2171.  
  2172.   Qdisabled = intern ("disabled");
  2173.   staticpro (&Qdisabled);
  2174.  
  2175.   defsubr (&Sread_key_sequence);
  2176.   defsubr (&Srecursive_edit);
  2177.   defsubr (&Sinput_pending_p);
  2178.   defsubr (&Scommand_execute);
  2179.   defsubr (&Srecent_keys);
  2180.   defsubr (&Sthis_command_keys);
  2181.   defsubr (&Ssuspend_emacs);
  2182.   defsubr (&Sabort_recursive_edit);
  2183.   defsubr (&Sexit_recursive_edit);
  2184.   defsubr (&Srecursion_depth);
  2185.   defsubr (&Stop_level);
  2186.   defsubr (&Sdiscard_input);
  2187.   defsubr (&Sopen_dribble_file);
  2188.   defsubr (&Sset_input_mode);
  2189.   defsubr (&Sexecute_extended_command);
  2190.  
  2191.   DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
  2192.     "Value is called instead of any command that is disabled\n\
  2193. \(has a non-nil  disabled  property).");
  2194.  
  2195.   DEFVAR_BOOL ("meta-flag", &meta_key,
  2196.     "*Non-nil means treat 0200 bit in terminal input as Meta bit.");
  2197.  
  2198.   DEFVAR_INT ("last-command-char", &last_command_char,
  2199.     "Last terminal input character that was part of a command, as an integer.");
  2200.  
  2201.   DEFVAR_INT ("last-input-char", &last_input_char,
  2202.     "Last terminal input character, as an integer.");
  2203.  
  2204.   DEFVAR_INT ("unread-command-char", &unread_command_char,
  2205.     "Character to be read as next input from command input stream, or -1 if none.");
  2206.  
  2207.   DEFVAR_INT ("meta-prefix-char", &meta_prefix_char,
  2208.     "Meta-prefix character code.  Meta-foo as command input\n\
  2209. turns into this character followed by foo.");
  2210.   meta_prefix_char = 033;
  2211.  
  2212.   DEFVAR_LISP ("last-command", &last_command,
  2213.     "The last command executed.  Normally a symbol with a function definition,\n\
  2214. but can be whatever was found in the keymap, or whatever the variable\n\
  2215. `this-command' was set to by that command.");
  2216.   last_command = Qnil;
  2217.  
  2218.   DEFVAR_LISP ("this-command", &this_command,
  2219.     "The command now being executed.\n\
  2220. The command can set this variable; whatever is put here\n\
  2221. will be in  last-command  during the following command.");
  2222.   this_command = Qnil;
  2223.  
  2224.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  2225.     "*Number of keyboard input characters between auto-saves.\n\
  2226. Zero means disable autosaving.");
  2227.   auto_save_interval = 300;
  2228.  
  2229.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  2230.     "*Nonzero means echo unfinished commands after this many seconds of pause.");
  2231.   echo_keystrokes = 1;
  2232.  
  2233.   DEFVAR_INT ("polling-period", &polling_period,
  2234.     "*Interval between polling for input during Lisp execution.\n\
  2235. The reason for polling is to make C-g work to stop a running program.\n\
  2236. Polling is needed only when using X windows and SIGIO does not work.\n\
  2237. Polling is automatically disabled in all other cases.");
  2238.   polling_period = 2;
  2239.  
  2240.   DEFVAR_INT ("help-char", &help_char,
  2241.     "Character to recognize as meaning Help.\n\
  2242. When it is read, do (eval help-form), and display result if it's a string.\n\
  2243. If help-form's value is nil, this char can be read normally.");
  2244.   help_char = Ctl ('H');
  2245.  
  2246.   DEFVAR_LISP ("help-form", &Vhelp_form,
  2247.     "Form to execute when character help-char is read.\n\
  2248. If the form returns a string, that string is displayed.\n\
  2249. If help-form is nil, the help char is not recognized.");
  2250.   Vhelp_form = Qnil;
  2251.   
  2252.   DEFVAR_LISP ("top-level", &Vtop_level,
  2253.     "Form to evaluate when Emacs starts up.\n\
  2254. Useful to set before you dump a modified Emacs.");
  2255.   Vtop_level = Qnil;
  2256.  
  2257.   DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
  2258.     "String used as translate table for keyboard input, or nil.\n\
  2259. Each character is looked up in this string and the contents used instead.\n\
  2260. If string is of length N, character codes N and up are untranslated."); 
  2261.   Vkeyboard_translate_table = Qnil;
  2262. }
  2263.  
  2264. keys_of_keyboard ()
  2265. {
  2266.   ndefkey (Vglobal_map, Ctl ('Z'), "suspend-emacs");
  2267.   ndefkey (Vctl_x_map, Ctl ('Z'), "suspend-emacs");
  2268.   ndefkey (Vesc_map, Ctl ('C'), "exit-recursive-edit");
  2269.   ndefkey (Vglobal_map, Ctl (']'), "abort-recursive-edit");
  2270.   ndefkey (Vesc_map, 'x', "execute-extended-command");
  2271. }
  2272.